package org.middleware.service.system;

import lombok.extern.slf4j.Slf4j;
import org.middleware.common.BusinessException;
import org.middleware.dao.system.SysSystemsDao;
import org.middleware.dao.system.SysUsersDao;
import org.middleware.entity.system.SysSystems;
import org.middleware.entity.system.SysUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhiqiangwu
 * @description 针对表【sys_systems(系统/服务表)】的数据库操作Service
 * @createDate 2025-09-05 16:40:01
 */
@Slf4j
@Service
public class SysSystemsService {
    @Autowired
    private SysSystemsDao systemsDao;

    @Autowired
    private SysUsersDao usersDao;

    public List<SysSystems> subscribers(int id) {
        SysUsers user = usersDao.getUserById(id);
        if (user.getIsSuperAdmin() == 1) {
            log.info("当前用户为系统管理员，查询所有系统");
            return systemsDao.getAllSystems();
        }
        // 非管理员，要查询企业订阅的和个人订阅的系统
        List<SysSystems> systems = systemsDao.getSubscribers(user);
        return systems;
    }

    public List<SysSystems> getAllSystems() {
        return systemsDao.getAllSystems();
    }

    public List<SysSystems> getSystemsByName(String name) {
        return systemsDao.getSystemsByName(name);
    }

    public int create(SysSystems system) {
        return systemsDao.create(system);
    }

    /**
     * 更新服务信息
     * @param system 服务数据
     * @return
     */
    public int update(SysSystems system) {
        if (system == null || system.getId() <= 0) {
            throw new BusinessException("服务信息不存在");
        }
        if (system.getName().trim().length() == 0) {
            throw new BusinessException("服务名不能为空");
        }
        return systemsDao.update(system);
    }

    /**
     * 删除服务id
     * @param id 服务id
     * @return
     */
    public int delete(int id) {
        if (id <= 0) {
            throw new BusinessException("服务id不能为空");
        }
        return systemsDao.delete(id);
    }
}
